<?php

namespace app\common\model;

use think\Model;
use think\Db;
class ViolationLog extends Model
{
    protected $resultSetType = '';
    
    public static function get_list($page,$keywords,$user_id,$vio_id)
    {
        $where = [];
        if($keywords)
        {
            $where['c.plate_number']=['like','%'.$keywords.'%'];
        }
        if($user_id)
        {
            $where['zhifa_id']=$user_id;
        }
        if($vio_id)
        {
            $where['violation_id']=$vio_id;
        }
        
        $field = "c.plate_number,s.store_name,v.*";
        $order = "v.id desc";
        $count = self::alias('v')->join('store s','v.store_id=s.id','left')->join('car c','v.car_id=c.id','left')->where($where)->count();
        $limit = 20;
        $start = ($page - 1) * $limit;
        $total = ceil($count / $limit);
        $list = self::alias('v')->join('store s','v.store_id=s.id','left')->join('car c','v.car_id=c.id','left')->where($where)->field($field)->order($order)->limit($start, $limit)->select();
        foreach ($list as $key => &$value) {
            # code...
            //查询项目
            $product=db('product')->where('id','in',$value['product_ids'])->column('title');
            $value['product']=implode(',',$product);
        }
        $data = [
            'total'=>$total,
            'list'=>$list
        ];
        return $data;
    }

    public static function get_info($id)
    {
        $where['id']=$id;
        $info = self::where($where)->field("*")->find();
        //图片
        $images=[];
        if($info['pic'])
        {
            $images=explode(',',$info['pic']);
            foreach ($images as $key => &$value) {
                # code...
                $value=gethost().$value;
            }
        }
        
        $info['imagelist']=$images;

        //获取车辆信息
        $info['plate_number']=db('car')->where(['id'=>$info['car_id']])->value('plate_number');
        //获取运输公司信息
        $info['store_name']=db('store')->where(['id'=>$info['store_id']])->value('store_name');
        //获取项目信息
        $product=db('product')->where('id','in',$info['product_ids'])->column('title');
        $info['product']=implode(',',$product);
        //获取执法人员信息
        $info['zhifa_name']=db('zhifa')->where(['id'=>$info['store_id']])->value('name');

        
        return $info;
    }

    public function getAddTimeAttr($value)
    {
        if($value){
            return date("Y-m-d H:i:s",$value);
        }else{
            return "";
        }
    }
    public function getEditTimeAttr($value)
    {
        if($value){
            return date("Y-m-d H:i:s",$value);
        }else{
            return "";
        }
    }
    
}
